//
//  SmallDecisionsTextField.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/26.
//

#import "SmallDecisionsTextField.h"

@interface SmallDecisionsTextField () <UITextFieldDelegate>

@end

@implementation SmallDecisionsTextField

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        self.font = SmallDecisionsSetFontSize(14);
        self.textColor = SmallDecisionsThemeFontColor;
        self.placeholder = @" ";
        self.placeholderColor = [UIColor colorWithHexString:@"0x999999"];
        [self addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
        self.delegate = self;
        self.enableInput = YES;
    }
    return self;
}

- (UIButton *)leadingViewBtn {
    if (!_leadingViewBtn) {
        _leadingViewBtn = [AlignButton buttonWithType:UIButtonTypeCustom];
        _leadingViewBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _leadingViewBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _leadingViewBtn.frame = self.leadingViewRect;
        self.leftView = _leadingViewBtn;
        self.leftViewMode = UITextFieldViewModeAlways;
    }
    return _leadingViewBtn;
}

- (void)setLeadingViewTitle:(NSString * _Nullable)title
                          font:(UIFont * _Nullable)font
                         color:(UIColor * _Nullable)color {
    
    [self.leadingViewBtn setTitle:title forState:UIControlStateNormal];
    [self.leadingViewBtn setTitle:title forState:UIControlStateHighlighted];
    [self.leadingViewBtn setTitleColor:color forState:UIControlStateNormal];
    [self.leadingViewBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    [self.leadingViewBtn setTitleColor:color forState:UIControlStateDisabled];
    if (font) {
        self.leadingViewBtn.titleLabel.font = font;
    }
}

- (UIButton *)trailingViewBtn {
    if (!_trailingViewBtn) {
        _trailingViewBtn = [AlignButton buttonWithType:UIButtonTypeCustom];
        _trailingViewBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _trailingViewBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _trailingViewBtn.frame = self.trailingViewRect;
        [_trailingViewBtn addTarget:self action:@selector(trailingViewBtnAction) forControlEvents:UIControlEventTouchUpInside];
        self.rightView = _trailingViewBtn;
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    return _trailingViewBtn;
}

- (void)trailingViewBtnAction {
    if (self.textFieldStartEditingAction) {
        
        self.textFieldStartEditingAction(self);
    }
}

- (void)setTrailingViewTitle:(NSString * _Nullable)title
                           font:(UIFont * _Nullable)font
                          color:(UIColor * _Nullable)color {
    
    [self.trailingViewBtn setTitle:title forState:UIControlStateNormal];
    [self.trailingViewBtn setTitle:title forState:UIControlStateHighlighted];
    [self.trailingViewBtn setTitleColor:color forState:UIControlStateNormal];
    [self.trailingViewBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    [self.trailingViewBtn setTitleColor:color forState:UIControlStateDisabled];
    if (font) {
        self.trailingViewBtn.titleLabel.font = font;
    }
}

- (UIView *)lineView {
    
    if (!_lineView) {
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = SmallDecisionsSecondBgColor;
        [self addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _lineView;
}

- (void)setPlaceholderFont:(UIFont *)placeholderFont {
    if (_placeholderFont != placeholderFont) {
        _placeholderFont = placeholderFont;
        Ivar ivar = class_getInstanceVariable([SmallDecisionsTextField class], "_placeholderLabel");
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.font = placeholderFont;
    }
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    if (_placeholderColor != placeholderColor) {
        _placeholderColor = placeholderColor;
        Ivar ivar = class_getInstanceVariable([SmallDecisionsTextField class], "_placeholderLabel");
        UILabel *placeholderLabel = object_getIvar(self, ivar);
        placeholderLabel.textColor = placeholderColor;
    }
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
    
    return self.leadingViewRect;
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
    
    CGRect rightViewRect = CGRectMake(bounds.size.width - self.trailingViewRect.origin.x - self.trailingViewRect.size.width, self.trailingViewRect.origin.y, self.trailingViewRect.size.width, self.trailingViewRect.size.height);
    return rightViewRect;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect rect = CGRectMake(self.leadingViewRect.size.width + self.leadingViewRect.origin.x + self.textEdgeInset.left, 0, bounds.size.width - self.leadingViewRect.size.width - self.leadingViewRect.origin.x - self.textEdgeInset.left - self.trailingViewRect.size.width - self.trailingViewRect.origin.x - self.textEdgeInset.right, bounds.size.height);
    
    return rect;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    
    return [self textRectForBounds:bounds];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    
    return [self textRectForBounds:bounds];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (self.textFieldStartEditingAction) {
        self.textFieldStartEditingAction(self);
    }
    return self.enableInput;
}

- (void)textFieldTextChange:(UITextField *)textField {
    if (self.textFieldDidChangeAction) {
        self.textFieldDidChangeAction(self, textField.text);
    }
}


@end
